<html>
<head>
<title>day06作业-作业-用代码实现商品描述功能.ets</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #52cb54;}
.s1 { color: #a9b7c6;}
.s2 { color: #bbb529;}
.s3 { color: #faa23d;}
.s4 { color: #db7e9b;}
.s5 { color: #e2da90;}
.s6 { color: #aa7dfc;}
.s7 { color: #b3e54c;}
</style>
</head>
<body bgcolor="#2b2b2b">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
day06作业-作业-用代码实现商品描述功能.ets</font>
</center></td></tr></table>
<pre><span class="s0">// //相对路径:./从当前文件出发去查找目标文件.当前文件所在文件夹 /表示进入某个文件夹里面</span>
<span class="s0">// //绝对路径：../从指定盘符触发去查找目标文件 ..当前文件的上一级文件夹 /表示进入某个文件夹里面</span>
<span class="s0">// //语法：import 变量名 from '路径'</span>
<span class="s0">// import { xxx, name, count } from '../models/model1'</span>
<span class="s0">//</span>
<span class="s0">// const xxx1 = new xxx('面')</span>
<span class="s0">// xxx1.myname()</span>
<span class="s0">// count</span>
<span class="s0">// name</span>

<span class="s2">@</span><span class="s1">Entry</span>
<span class="s2">@</span><span class="s1">Component</span>
<span class="s1">struct Index </span><span class="s3">{</span>
  <span class="s1">build</span><span class="s4">() </span><span class="s3">{</span>

  <span class="s3">}</span>
<span class="s3">}</span>

<span class="s0">//定义接口IProduct，标记商品的通用信息</span>
<span class="s1">interface IProduct </span><span class="s3">{</span>
  <span class="s1">name</span><span class="s5">: </span><span class="s1">string</span>
  <span class="s1">price</span><span class="s5">: </span><span class="s1">number</span>
  <span class="s1">description</span><span class="s5">: </span><span class="s1">string</span>
<span class="s3">}</span>

<span class="s0">//定义类ProductList</span>
<span class="s1">class ProductList</span><span class="s5">&lt;</span><span class="s1">T extends IProduct</span><span class="s5">&gt; </span><span class="s3">{</span>
  <span class="s1">private products</span><span class="s5">: </span><span class="s1">T</span><span class="s4">[]</span>

  <span class="s1">constructor</span><span class="s4">(</span><span class="s1">products</span><span class="s5">: </span><span class="s1">T</span><span class="s4">[]) </span><span class="s3">{</span>
    <span class="s1">this</span><span class="s5">.</span><span class="s1">products </span><span class="s5">= </span><span class="s1">products</span>
  <span class="s3">}</span>

  <span class="s1">reder</span><span class="s4">() </span><span class="s3">{</span>
    <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s4">(</span><span class="s6">'商品列表：'</span><span class="s4">)</span>
    <span class="s1">for </span><span class="s4">(</span><span class="s1">let index </span><span class="s5">= </span><span class="s7">0</span><span class="s5">; </span><span class="s1">index </span><span class="s5">&lt; </span><span class="s1">this</span><span class="s5">.</span><span class="s1">products</span><span class="s5">.</span><span class="s1">length</span><span class="s5">; </span><span class="s1">index</span><span class="s5">++</span><span class="s4">) </span><span class="s3">{</span>
      <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s4">(</span><span class="s6">`商品序号</span><span class="s3">${</span><span class="s1">index </span><span class="s5">+ </span><span class="s7">1</span><span class="s3">}</span><span class="s6">:`</span><span class="s4">)</span>
      <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s4">(</span><span class="s6">'商品名：'</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">products</span><span class="s4">[</span><span class="s1">index</span><span class="s4">]</span><span class="s5">.</span><span class="s1">name</span><span class="s4">)</span>
      <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s4">(</span><span class="s6">'商品价格：'</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">products</span><span class="s4">[</span><span class="s1">index</span><span class="s4">]</span><span class="s5">.</span><span class="s1">price</span><span class="s4">)</span>
      <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s4">(</span><span class="s6">'商品描述：'</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">products</span><span class="s4">[</span><span class="s1">index</span><span class="s4">]</span><span class="s5">.</span><span class="s1">description</span><span class="s4">)</span>
      <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s4">(</span><span class="s6">'分割线-------------------------------'</span><span class="s4">)</span>
    <span class="s3">}</span>
  <span class="s3">}</span>
<span class="s3">}</span>

<span class="s0">//定义接口Book</span>
<span class="s1">interface Book extends IProduct </span><span class="s3">{</span>
  <span class="s1">author</span><span class="s5">: </span><span class="s1">string</span>
  <span class="s1">pages</span><span class="s5">: </span><span class="s1">number</span>
<span class="s3">}</span>

<span class="s0">//定义接口Clothing</span>
<span class="s1">interface Clothing extends IProduct </span><span class="s3">{</span>
  <span class="s1">size</span><span class="s5">: </span><span class="s1">string</span>
  <span class="s1">color</span><span class="s5">: </span><span class="s1">string</span>
<span class="s3">}</span>

<span class="s1">const books</span><span class="s5">: </span><span class="s1">Book</span><span class="s4">[] </span><span class="s5">= </span><span class="s4">[</span>
  <span class="s3">{</span>
    <span class="s1">name</span><span class="s5">: </span><span class="s6">&quot;三国演义&quot;</span><span class="s5">,</span>
    <span class="s1">price</span><span class="s5">: </span><span class="s7">10</span><span class="s5">,</span>
    <span class="s1">description</span><span class="s5">: </span><span class="s6">&quot;《三国演义》与《西游记》《水浒传》《红楼梦》并称为中国古典四大名著。&quot;</span><span class="s5">,</span>
    <span class="s1">author</span><span class="s5">: </span><span class="s6">&quot;罗贯中&quot;</span><span class="s5">,</span>
    <span class="s1">pages</span><span class="s5">: </span><span class="s7">688</span>
  <span class="s3">}</span><span class="s5">,</span>
  <span class="s3">{</span>
    <span class="s1">name</span><span class="s5">: </span><span class="s6">&quot;西游记 &quot;</span><span class="s5">,</span>
    <span class="s1">price</span><span class="s5">: </span><span class="s7">12</span><span class="s5">,</span>
    <span class="s1">description</span><span class="s5">: </span><span class="s6">&quot;西游记》是中国古代第一部浪漫主义章回体长篇神魔小说&quot;</span><span class="s5">,</span>
    <span class="s1">author</span><span class="s5">: </span><span class="s6">&quot;吴承恩&quot;</span><span class="s5">,</span>
    <span class="s1">pages</span><span class="s5">: </span><span class="s7">500</span>
  <span class="s3">}</span>
<span class="s4">]</span><span class="s5">;</span>
<span class="s1">const productList </span><span class="s5">= </span><span class="s1">new ProductList</span><span class="s5">&lt;</span><span class="s1">Book</span><span class="s5">&gt;</span><span class="s4">(</span><span class="s1">books</span><span class="s4">)</span>
<span class="s1">productList</span><span class="s5">.</span><span class="s1">reder</span><span class="s4">()</span>
<span class="s1">const clothing</span><span class="s5">: </span><span class="s1">Clothing</span><span class="s4">[] </span><span class="s5">= </span><span class="s4">[</span>
  <span class="s3">{</span>
    <span class="s1">name</span><span class="s5">: </span><span class="s6">&quot;T恤&quot;</span><span class="s5">,</span>
    <span class="s1">price</span><span class="s5">: </span><span class="s7">15</span><span class="s5">,</span>
    <span class="s1">description</span><span class="s5">: </span><span class="s6">&quot;T恤（体恤），又称T恤衫（体恤衫）、丅字衫，是春夏季人们最喜欢的服装之一&quot;</span><span class="s5">,</span>
    <span class="s1">size</span><span class="s5">: </span><span class="s6">&quot;M&quot;</span><span class="s5">,</span>
    <span class="s1">color</span><span class="s5">: </span><span class="s6">&quot;Blue&quot;</span>
  <span class="s3">}</span><span class="s5">,</span>
  <span class="s3">{</span>
    <span class="s1">name</span><span class="s5">: </span><span class="s6">&quot;马甲&quot;</span><span class="s5">,</span>
    <span class="s1">price</span><span class="s5">: </span><span class="s7">40</span><span class="s5">,</span>
    <span class="s1">description</span><span class="s5">: </span><span class="s6">&quot;马甲是一种无领无袖，且较短的上衣。&quot;</span><span class="s5">,</span>
    <span class="s1">size</span><span class="s5">: </span><span class="s6">&quot;L&quot;</span><span class="s5">,</span>
    <span class="s1">color</span><span class="s5">: </span><span class="s6">&quot;Black&quot;</span>
  <span class="s3">}</span>
<span class="s4">]</span>
<span class="s1">const productList1 </span><span class="s5">= </span><span class="s1">new ProductList</span><span class="s5">&lt;</span><span class="s1">Clothing</span><span class="s5">&gt;</span><span class="s4">(</span><span class="s1">clothing</span><span class="s4">)</span>
<span class="s1">productList1</span><span class="s5">.</span><span class="s1">reder</span><span class="s4">()</span>
</pre>
</body>
</html>